using UnityEngine;
using UnityEngine.AI;

public class Trojan : Enemy
{
    public Vector3 targetPosition;
    NavMeshAgent agent;

    public float range = 0.1f;
    public LayerMask targetLayer; // 目标层

    void Start()
    {
        //保证生成地点在导航上
        agent = GetComponent<NavMeshAgent>();

        NavMeshHit hit;
        if(NavMesh.SamplePosition(targetPosition,out hit,2.0f,NavMesh.AllAreas)){
            targetPosition = hit.position;
        }
        else{
            Debug.LogError("No NavMesh found near the target position.");
        }
        Move();
    }

    void Update()
    {
        AttackHost();
    }
    public override void Move()
    {
        // 实现木马移动的逻辑
        agent.destination = targetPosition;
    }
    public override void TakeDamage(int damage){
        health -= damage;
        if(health <= 0){
            Die();
            HostDate host = GameObject.Find("Host").GetComponent<HostDate>();
            host.AddAssets(20);
        }

    }
    public override void AttackHost()
    {
        // 实现木马攻击宿主的逻辑
        
        //攻击主机
        RaycastHit hit;
        if (Physics.Raycast(transform.position, transform.forward, out hit, range, targetLayer))
        {
            Debug.Log("Trojan attacking host!");
            //后续添加对Host的伤害属性
            Destroy(gameObject); // 销毁目标
        }
    }

    
}
